package com.sylli.oeuf.server.db.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.exception.ExceptionCause;
import com.sylli.oeuf.server.object.transferable.MapMonsterTO;

public class MySQLMapMonster extends MySQLBasedProvider<MapMonsterTO> {

	public MySQLMapMonster() {
		interactTableName = "map_monster";
	}
	
	@Override
	protected PreparedStatement convertObjectToInsertQuery(MapMonsterTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"INSERT INTO " + interactTableName 
					+ " (block_id, map_id, monster_id, spawn_perc)"
					+ " VALUES (?,?,?,?)", 
					Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1, obj.getBlockId());
			ps.setInt(2, obj.getMapId());
			ps.setInt(3, obj.getMonsterId());
			ps.setFloat(4, obj.getSpawnPerc());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		}
	}

	@Override
	protected PreparedStatement convertObjectToUpdateQuery(MapMonsterTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"UPDATE " + interactTableName 
					+ " SET spawn_perc=? WHERE block_id = ? AND map_id=? AND monster_id=?;");
			ps.setFloat(1, obj.getSpawnPerc());
			ps.setInt(2, obj.getBlockId());
			ps.setInt(3, obj.getMapId());
			ps.setInt(4, obj.getMonsterId());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		}
	}

	@Override
	protected MapMonsterTO convertResultSetToObject(ResultSet rs)
			throws BasedException {
		try {
			MapMonsterTO sto = new MapMonsterTO();
			rs.first();
			sto.setBlockId(rs.getInt(1));
			sto.setMapId(rs.getInt(2));
			sto.setMonsterId(rs.getInt(3));
			sto.setSpawnPerc(rs.getFloat(4));
			return sto;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		}
	}

	@Override
	protected Collection<MapMonsterTO> convertResultSetToObjects(ResultSet rs)
			throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			Collection<MapMonsterTO> objs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

}
